package dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import pojo.Book;
import util.DBUtils;
import dao.BookDao;

public class BookDaoImpl implements BookDao {

	public int addBook(Book book) {
		String sql = "insert into books(id,bname,publishDate,author,price,unused,publisher) values(bseq.nextval,?,?,?,?,?,?)";
		PreparedStatement pst = null;
		int m = 0;
		Connection conn = DBUtils.createConnectionWithDataSource();
		try {
			pst = conn.prepareStatement(sql);
			pst.setString(1, book.getBname());
			pst.setDate(2, new java.sql.Date(book.getPublishDate().getTime()));
			pst.setString(3, book.getAuthor());
			pst.setDouble(4, book.getPrice());
			pst.setBoolean(5, book.isUnused());
			pst.setString(6, book.getPublisher());
			m = pst.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBUtils.close(conn, pst, null);
		}
		return m;
	}

	public int deleteBook(Integer id) {
		String sql = "delete books where id=?";
		Connection conn = DBUtils.createConnectionWithDataSource();
		PreparedStatement pst = null;
		int m = 0;
		try {
			pst = conn.prepareStatement(sql);
			pst.setInt(1, id);
			m = pst.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DBUtils.close(conn, pst, null);
		}
		return m;
	}

	public int updateBook(Book book) {
		String sql = "update books set bname=?,publishDate=?,author=?,price=?,unused=?,publisher=? where id =?";
		Connection conn = DBUtils.createConnectionWithDataSource();
		PreparedStatement pst = null;
		int m = 0;
		try {
			pst = conn.prepareStatement(sql);
			pst.setString(1, book.getBname());
			pst.setDate(2, new java.sql.Date(book.getPublishDate().getTime()));
			pst.setString(3, book.getAuthor());
			pst.setDouble(4, book.getPrice());
			pst.setBoolean(5, book.isUnused());
			pst.setString(6, book.getPublisher());
			pst.setInt(7, book.getId());
			m = pst.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DBUtils.close(conn, pst, null);
		}
		return m;
	}

	public Book loadBook(Integer id) {
		String sql = "select * from books where id=?";
		Connection conn = DBUtils.createConnectionWithDataSource();
		PreparedStatement pst = null;
		ResultSet rs = null;
		Book book =null;
		try {
			pst = conn.prepareStatement(sql);
			pst.setInt(1, id);
			rs = pst.executeQuery();
			if (rs.next()) {
				book = new Book();
				book.setId(rs.getInt("id"));
				book.setBname(rs.getString("bname"));
				book.setPublishDate(new Date(rs.getDate("publishDate").getTime()));
				book.setAuthor(rs.getString("author"));
				book.setPrice(rs.getDouble("price"));
				book.setUnused(rs.getBoolean("unused"));
				book.setPublisher(rs.getString("publisher"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DBUtils.close(conn, pst, rs);
		}
		return book;
	}

	public List<Book> findAll() {
		List<Book> books = new ArrayList<Book>();
		String sql = "select * from books";
		Connection conn = DBUtils.createConnectionWithDataSource();
		PreparedStatement pst = null;
		ResultSet rs = null;
		try {
			pst = conn.prepareStatement(sql);
			rs = pst.executeQuery();
			if (rs.next()) {
				Book book = new Book();
				book.setId(rs.getInt("id"));
				book.setBname(rs.getString("bname"));
				book.setPublishDate(new Date(rs.getDate("publishDate").getTime()));
				book.setAuthor(rs.getString("author"));
				book.setPrice(rs.getDouble("price"));
				book.setUnused(rs.getBoolean("unused"));
				book.setPublisher(rs.getString("publisher"));
				books.add(book);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DBUtils.close(conn, pst, rs);
		}
		return books;
	}

}
